
package net.ck.hzq.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import event.PosAllotEvent;


/**
 *POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class PosAllotAdapter extends DelegateAdapter.Adapter<PosAllotAdapter.ProfitVH> {

    private final Context mContext;
    private List<String>  data;//数据
    private List<String>  snsdata;//sns数据
    private List<String> mSelectedDevices = new ArrayList<>();
    private HashMap<String, String> backAmountMap;
    private List<String>  selectPosition = new ArrayList<>();//选择的条目
    private int mMaxBackAmount = 0;

    public PosAllotAdapter(Context context, List<String>  mdata,List<String> msnsdata, HashMap<String, String> mBackAmountMap) {
        mContext = context;
        data = mdata;
        snsdata = msnsdata;
        backAmountMap = mBackAmountMap;
    }



    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public ProfitVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProfitVH(LayoutInflater.from(mContext).inflate(R.layout.item_choose_device_list, viewGroup, false));
    }




    @Override
    public void onBindViewHolder(@NonNull ProfitVH holder, int position) {
        if (position < 0 || position >= data.size()) {
            return;
        }

        holder.index.setText(position + 1 + "");
        holder.deviceNo.setText(data.get(position));
        String amount = backAmountMap.get(data.get(position));
        try {
            mMaxBackAmount = Math.max(mMaxBackAmount, Math.round(Float.valueOf(amount)));
            holder.returnAmountRange.setText("0-" + Math.round(Float.valueOf(amount)));
        }catch (Exception e){

        }


        if (mSelectedDevices.indexOf(data.get(position)) > -1) {
            holder.cb.setBackgroundResource(R.mipmap.checkedbox);
        } else {
            holder.cb.setBackgroundResource(R.mipmap.checkbox);
        }
        holder.layout4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mSelectedDevices.indexOf(data.get(position)) < 0) {
                    mSelectedDevices.add(data.get(position));
                    selectPosition.add(position + "");
                    holder.cb.setBackgroundResource(R.mipmap.checkedbox);
                    if (backAmountMap.get(data.get(position)) != null) {
                        if (mMaxBackAmount == 0&&mSelectedDevices.size()>0) {
                            mMaxBackAmount = Math.round(Float.valueOf(backAmountMap.get(data.get(position))));
                        } else {
                            mMaxBackAmount = Math.min(mMaxBackAmount, Math.round(Float.valueOf(backAmountMap.get(data.get(position)))));
                        }
                    }

                } else {
                    mSelectedDevices.remove(data.get(position));
                    selectPosition.remove(position + "");
                    holder.cb.setBackgroundResource(R.mipmap.checkbox);
                    if(mSelectedDevices.size()==0){
                        mMaxBackAmount = 0;
                    }
                }
                int tag = 0;
                for (int i =0;i<mSelectedDevices.size();i++){
                    int positionnum = Math.round(Float.valueOf(backAmountMap.get(mSelectedDevices.get(i))));
                    if(tag>positionnum||tag == 0){
                        tag = positionnum;
                    }
                }
                mMaxBackAmount = tag;
                EventBus.getDefault().post(new PosAllotEvent(mMaxBackAmount, mSelectedDevices));
            }
        });
    }
    @Override
    public int getItemCount() {
        return data.size();
    }

    class ProfitVH extends RecyclerView.ViewHolder {
        TextView index;
        TextView deviceNo;;
        TextView returnAmountRange;
        ImageView cb;
        RelativeLayout layout4;

        ProfitVH(@NonNull View itemView) {
            super(itemView);
            layout4 = itemView.findViewById(R.id.layout4);
            index = itemView.findViewById(R.id.index);
            deviceNo = itemView.findViewById(R.id.device_no);
            returnAmountRange = itemView.findViewById(R.id.return_amount_range);
            cb = itemView.findViewById(R.id.cb);

        }
    }
}
